package com.yunquan.ohana.ui.calendar.event.search.adapter

import android.graphics.Color
import android.view.Gravity
import android.widget.FrameLayout
import androidx.core.content.ContextCompat
import androidx.core.graphics.toColorInt
import androidx.databinding.ViewDataBinding
import com.yunquan.ohana.R
import com.yunquan.ohana.databinding.ItemSearchEventBinding
import com.yunquan.ohana.ui.calendar.event.search.EventSearchItemViewModel
import com.yunquan.ohana.view.CircleTextView
import me.tatarka.bindingcollectionadapter2.BindingRecyclerViewAdapter

class EventSearchAdapter : BindingRecyclerViewAdapter<EventSearchItemViewModel>() {

    override fun onBindBinding(binding: ViewDataBinding, variableId: Int, layoutRes: Int, position: Int, item: EventSearchItemViewModel?) {
        super.onBindBinding(binding, variableId, layoutRes, position, item)
        if (binding is ItemSearchEventBinding) {
            val resources = binding.root.context.resources
            // 设置背景色
            val roles = item?.data?.scheduleToRoleVos ?: mutableListOf()
            val isSingleRole = roles.size <= 1
            val firstRole = roles.first()
            val outColor = if (isSingleRole) {
                firstRole.getColorData().inner.toColorInt()
            } else {
                // 如果有多个角色
                ContextCompat.getColor(binding.root.context, R.color.c_54)
            }
            val innerColor = if (isSingleRole) {
                firstRole.getColorData().outer.toColorInt()
            } else {
                ContextCompat.getColor(binding.root.context, R.color.white)
            }
            val borderColor = if (isSingleRole) {
                ContextCompat.getColor(binding.root.context, R.color.transparent)
            } else {
                ContextCompat.getColor(binding.root.context, R.color.c_C3C3C3)
            }
            binding.vOut.helper.backgroundColorNormal = outColor
            binding.clInner.helper.backgroundColorNormal = innerColor
            binding.clRoot.helper.borderColorNormal = borderColor
            // 添加角色
            binding.llRole.removeAllViews()
            val chipSize = binding.llRole.layoutParams.height
            val steep = chipSize / 5 * 2
            roles.forEachIndexed { index, roleModel ->
                val roleName = roleModel.roleName?.firstOrNull()?.toString() ?: ""
                val tvRole = CircleTextView(binding.root.context)
                val layoutParams = FrameLayout.LayoutParams(chipSize, chipSize)
                layoutParams.gravity = Gravity.END
                layoutParams.rightMargin = index * chipSize - index * steep
                tvRole.layoutParams = layoutParams
                tvRole.setCircleText(roleName)
                tvRole.setCircleTextColor(Color.WHITE)
                tvRole.setCircleColor(roleModel.getColorData().inner.toColorInt())
                tvRole.setOuterCircleColor(Color.WHITE)
                tvRole.circleRadius = resources.getDimension(R.dimen.dp_9)
                tvRole.outerCircleRadius = resources.getDimension(R.dimen.dp_10)
                tvRole.circleTextSize = resources.getDimension(R.dimen.sp_12)

                binding.llRole.addView(tvRole, index)
            }
            if (roles.size > 1) {
                val roleName = "+${roles.size}"
                val tvRole = CircleTextView(binding.root.context)
                val layoutParams = FrameLayout.LayoutParams(chipSize, chipSize)
                layoutParams.gravity = Gravity.END
                layoutParams.rightMargin = roles.size * chipSize - roles.size * steep
                tvRole.layoutParams = layoutParams
                tvRole.setCircleText(roleName)
                tvRole.setCircleTextColor(ContextCompat.getColor(binding.root.context, R.color.c_54))
                tvRole.setCircleColor(ContextCompat.getColor(binding.root.context, R.color.c_d3))
                tvRole.setOuterCircleColor(Color.WHITE)
                tvRole.circleRadius = resources.getDimension(R.dimen.dp_9)
                tvRole.outerCircleRadius = resources.getDimension(R.dimen.dp_10)
                tvRole.circleTextSize = resources.getDimension(R.dimen.sp_12)

                binding.llRole.addView(tvRole, roles.size)
            }
        }
    }
}